/*** Eclipse Class Decompiler plugin, copyright (c) 2016 Chen Chao (cnfree2000@hotmail.com) ***/
package com.shiro.util;

import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import oracle.sql.CLOB;

public class ResultMap<K, V> {
	private Map<K, V> map;

	public ResultMap() {
		this.map = new ConcurrentHashMap<K, V>();
	}


	public ResultMap(Map<K, V> map) {
		putAll(map);
	}


	public int size() {
		return this.map.size();
	}

	public boolean isEmpty() {
		return this.map.isEmpty();
	}

	public boolean containsKey(Object key) {
		return this.map.containsKey(toLowerCase(key));
	}

	@SuppressWarnings("unchecked")
	private K toLowerCase(Object key) {
		if ((key != null) && (key instanceof String)) {
			key = ((String) key).toLowerCase();
		}
		return (K) key;
	}

	public boolean containsValue(Object value) {
		return this.map.containsValue(value);
	}

	public V get(Object key) {
		return this.map.get(toLowerCase(key));
	}

	@SuppressWarnings("unchecked")
	public V put(Object key, Object value) {
		if ((value != null) && (value instanceof CLOB)) {
			try {
				int size = (int) ((CLOB) value).length();

				value = ((CLOB) value).getSubString(1L, size);
			} catch (Exception ex) {
				ex.printStackTrace();
			}

		}

		/*if ((value != null) && (value instanceof Date)) {
			try {
				value = DateUtil.convertGMTTimestamp(DateUtil.getDateyyyyMMddHHmmss((Date) value));
			} catch (Exception ex) {
				ex.printStackTrace();
			}

		}*/

		if (value != null) {
			return this.map.put(toLowerCase(key), (V) value);
		}
		return null;
	}

	public V remove(Object key) {
		return this.map.remove(toLowerCase(key));
	}



	@SuppressWarnings("rawtypes")
	public void putAll(Map<? extends K, ? extends V> m) {
		for (Iterator i = m.entrySet().iterator(); i.hasNext();) {
			Map.Entry e = (Map.Entry) i.next();
			put(e.getKey(), e.getValue());
		}
	}

	public void clear() {
		this.map.clear();
	}

	public Set<K> keySet() {
		return this.map.keySet();
	}

	public Collection<V> values() {
		return this.map.values();
	}

	public Set<Map.Entry<K, V>> entrySet() {
		return this.map.entrySet();
	}

	public String toString() {
		return this.map.toString();
	}


	public void setAttribute(String name, String value) {
		put(name, value);
	}
}